cmake_minimum_required(VERSION 3.0.0)
project(bulingbuling VERSION 0.1.0)
#生成的可执行文件要比 直接make 的大些 可能是 生成.o 时加了  -m -d (要验证)
# 设置交叉编译器
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
set(CMAKE_OBJCOPY arm-linux-gnueabihf-objcopy)
set(CMAKE_OBJDUMP arm-linux-gnueabihf-objdump)
message("${CMAKE_SOURCE_DIR}")
message(${CMAKE_SOURCE_DIR})

# 包含目录
include_directories(
    imx6ul
    bsp/clk
    bsp/led
    bsp/delay
)

# 源文件列表
file(GLOB_RECURSE SRC_FILES 
    "${CMAKE_SOURCE_DIR}/project/*.c"
    "${CMAKE_SOURCE_DIR}/bsp/clk/*.c"
    "${CMAKE_SOURCE_DIR}/bsp/led/*.c"
    "${CMAKE_SOURCE_DIR}/bsp/delay/*.c"
)

add_custom_command(
    OUTPUT createObj 
    COMMAND mkdir -p ${CMAKE_BINARY_DIR}/obj
    COMMENT "obj 文件夹"
)

add_custom_command(
    OUTPUT start 
    COMMAND arm-linux-gnueabihf-gcc -g -c ${CMAKE_SOURCE_DIR}/project/start.S -o ${CMAKE_BINARY_DIR}/obj/start.o
    COMMENT "生成 start.o"
    DEPENDS createObj
)

# 定义目标
add_custom_target(hi ALL DEPENDS start)
# 指定编译选项
add_compile_options(-Wall -nostdlib -O2)

add_executable(${PROJECT_NAME}.elf ${SRC_FILES})

# 链接脚本
set_target_properties(${PROJECT_NAME}.elf PROPERTIES LINK_FLAGS "-nostartfiles -T ${CMAKE_SOURCE_DIR}/imx6ul.lds")

# 添加自定义命令，生成二进制文件和反汇编文件
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary -S ${PROJECT_NAME}.elf ${PROJECT_NAME}.bin
    COMMAND ${CMAKE_OBJDUMP} -D -m arm ${PROJECT_NAME}.elf > ${PROJECT_NAME}.dis
    COMMENT "Generating ${PROJECT_NAME}.bin and ${PROJECT_NAME}.dis"
)

# # 清理目标
add_custom_target(clean-all
   COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/CleanAll.cmake
)
